
package ihm.util;

import javax.faces.model.DataModel;

/**
 *
 * @author johann
 */
public abstract class AidePagination {

    private int pageSize;
    private int page;

    /**
     *
     * @param pageSize
     */
    public AidePagination(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     *
     * @return
     */
    public abstract int getItemsCount();

    /**
     *
     * @return
     */
    public abstract int getItemsCountOuvert();

    /**
     *
     * @return
     */
    public abstract DataModel createPageDataModel();

    /**
     *
     * @return
     */
    public abstract DataModel createPageDataModelOuvert();

    /**
     *
     * @return
     */
    public int getPageFirstItem() {
        return page*pageSize;
    }

    /**
     *
     * @return
     */
    public int getPageLastItem() {
        int i = getPageFirstItem() + pageSize -1;
        int count = getItemsCount() - 1;
        if (i > count) {
            i = count;
        }
        if (i < 0) {
            i = 0;
        }
        return i;
    }

     /**
     *
     * @return
     */
    public int getPageLastItemOuvert() {
        int i = getPageFirstItem() + pageSize -1;
        int count = getItemsCountOuvert() - 1;
        if (i > count) {
            i = count;
        }
        if (i < 0) {
            i = 0;
        }
        return i;
    }

    /**
     *
     * @return
     */
    public boolean isHasNextPage() {
        return (page+1)*pageSize+1 <= getItemsCount();
    }

    /**
     *
     */
    public void nextPage() {
        if (isHasNextPage()) {
            page++;
        }
    }

    /**
     *
     */
    public void setFirstPage(){
        page = 0;
    }

    /**
     *
     * @return
     */
    public boolean isHasPreviousPage() {
        if (getItemsCount() < (page*pageSize))
            setFirstPage();
        return page > 0;
    }

    /**
     *
     */
    public void previousPage() {
        if (isHasPreviousPage()) {
            page--;
        }
    }

    /**
     *
     * @return
     */
    public int getPageSize() {
        return pageSize;
    }

}
